package 力扣._1_两数之和;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Solution {
    public int[] twoSum(int[] nums, int target) {
        //nums有重复元素，但不可选选择下标相同的元素

        int[] r = new int[2];
        Map<Integer, List<Integer>> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            List<Integer> indexes = map.getOrDefault(nums[i],new ArrayList<>());
            indexes.add(i);
            map.put(nums[i], indexes);
        }

        for (int i = 0; i < nums.length; i++) {
            int t = target - nums[i];
            if (map.containsKey(t)) {
                List<Integer> indexes = map.get(t);
                for (int index : indexes) {
                    if (index != i) {
                        r[0] = i;
                        r[1] = index;
                    }
                }
            }
        }
        return r;
    }
}
